home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12333 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: mother.usf.edu!news
  2. From: yatesc@csee.usf.edu (Randy Yates)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: what getline returns? Nobody can explain this!!
  5. Date: 19 Mar 1996 15:20:51 GMT
  6. Organization: University of South Florida
  7. Distribution: na
  8. Message-ID: <4imjcj$cf3@mother.usf.edu>
  9. References: <4ifbk8$6nf@bcarh8ab.bnr.ca>
  10. NNTP-Posting-Host: ppp129.cfr.usf.edu
  11. Mime-Version: 1.0
  12. X-Newsreader: WinVN 0.93.14
  13.  
  14. In article <4ifbk8$6nf@bcarh8ab.bnr.ca>, liyu@bnr.ca says...
  15. >
  16. >getline member fuction in ifstream is supposed to return the
  17. >receiving object (that is, ostream&). 
  18. >
  19. >However, many books contain the code samples like:
  20. >
  21. >ifstream in("tmp.txt");
  22. >while (in.getline(buffer,sizeof(buffer))) { ...  }
  23. >
  24. >This loop quits only if in.getline returns 0 or NUll;
  25. >                ^^^^^^^
  26. >
  27. >but an ostream object is neither 0 nor NULL;
  28. >the result is an object, not integer or pointer.
  29. >
  30. >So how can this loop quit?
  31. >
  32. >It works; but no one I know can explain this!!!!
  33. >Thanks for your help.
  34.  
  35. First of all, the getline member function returns an 
  36. istream, not ostream. 
  37.  
  38. Are you sure the code is not this:
  39.  
  40. while (!in.getline(buffer,sizeof(buffer))) { ...  }
  41.  
  42. I did a little research since my last post. What's happening
  43. is that the ios class (from which istream is derived) overloads
  44. the "!" operator to return the "fail" flag, which indicates
  45. whether or not the last I/O operation failed. 
  46. -- 
  47. % Randy Yates                  % "...the answer lies within your soul
  48. % EE/Mathematics Student       %       'cause no one knows which side 
  49. % University of South Florida  %                   the coin will fall."
  50. % <yatesc@csee.usf.edu>        %  'Big Wheels', *Out of the Blue*, ELO   
  51.  
  52.